const dotenv = require("dotenv");
dotenv.config({ path: `.env.${process.env.NODE_ENV}` });
dotenv.config({ path: `.env` });

const express = require("express");
const app = express();
const cors = require("cors");

// 配置
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// 处理无效的属性值
app.use(require("./middleware/clearInvalidValueMiddleware"));
// 权限中间件
app.use(require("./middleware/permissionsMiddleware"));
// 路由
app.use("/api", require("./router/index"));
// 监听错误
app.use((err, req, res, next) => {
	console.log({ ...err, error: err.error.message });
});

app.listen(process.env.PORT, () => {
	console.log(`服务已启动, 端口${process.env.PORT}.`);
});
